package proc

import (
	"github.com/hu17889/go_spider/core/common/page"
	corerequest "github.com/hu17889/go_spider/core/common/request"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
	"net/http"
	"strconv"
	"tesou.io/platform/poem-parent/poem-api/common/base"
	"tesou.io/platform/poem-parent/poem-api/module/core/enums"
	"tesou.io/platform/poem-parent/poem-api/module/core/pojo"
	"tesou.io/platform/poem-parent/poem-core/module/core/service"
)

type TouTiaoProcesser struct {
	TouTiaoService    service.TouTiaoService
	SpiderUrl string
	cookies []*http.Cookie
}

func GetTouTiaoProcesser() *TouTiaoProcesser {
	return &TouTiaoProcesser{}
}

func (this *TouTiaoProcesser) Startup() {
	newSpider := spider.NewSpider(this, "TouTiaoProcesser")

	//newSpider = newSpider.AddUrl(this.SpiderUrl, "html")
	base.Log.Info("start URL:,", this.SpiderUrl)
	hder := make(http.Header)
	hder.Add("User-Agent", `Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36`)
	hder.Add("Upgrade-Insecure-Requests", `1`)
	hder.Add("x-tt-logid", `202106221535540102121450460100050E`)
	hder.Add("x-tt-trace-host", `01b1e635e30c0a6b7f383a4b9b196a1ce01106c4ad8080ff8ff69898ed17feaf68a5fce09c6450f25a06562277d21060129a2b42acf13064b7a0dd13c465148284fffa8e854c453e58a3b804b330a3e4484a5a583be9be2fee84ca0765ecf98711`)

	req := corerequest.NewRequest(this.SpiderUrl, "json", "jandanindex", "GET", "", hder, nil, nil,nil)
	newSpider.AddRequest(req)

	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
	newSpider.SetThreadnum(1).Run()
}

func (this *TouTiaoProcesser) Process(p *page.Page) {
	request := p.GetRequest()
	if !p.IsSucc() {
		base.Log.Info("URL:,", request.Url, p.Errormsg())
		return
	}
	//var allData []map[string]interface{}
	zhi_list_slice := make([]interface{}, 0)
	zhi_list_update_slice := make([]interface{}, 0)
	level := enums.GetListLevel("今日头条")

	query := p.GetJson()
	base.Log.Info("query:,", query)
	status, err := query.GetPath( "status" ).String()
	if   err != nil {
		base.Log.Info("URL:,", err)
	}
	base.Log.Info(status)
	query = query.GetPath( "data" )
	for i := 0; i < 50; i++ {
		ListId, err := query.GetIndex(i).Get("ClusterId").Int()
		if   err != nil {
			base.Log.Info(err)
		}
		base.Log.Info(ListId)
		SUrl,err := query.GetIndex(i).Get("Url").String()
		Title,err := query.GetIndex(i).Get("Title").String()
		Image,err := query.GetIndex(i).Get("LabelUri").Get("url").String()

		zhi := new(pojo.List)
		zhi.ListId = strconv.Itoa(ListId)
		zhi.Title = Title
		zhi.SUrl = SUrl
		zhi.Image = Image
		zhi.Content = ""

		if len(zhi.Title) > 0 {
			//设置朝代
			zhi.Type = level
			exist := this.TouTiaoService.Exist(zhi)
			if !exist {
				zhi_list_slice = append(zhi_list_slice, zhi)
			} else {
				zhi_list_update_slice = append(zhi_list_update_slice, zhi)
			}
		}
		i++
	}


	//执行入库
	this.TouTiaoService.SaveList(zhi_list_slice)
	this.TouTiaoService.ModifyList(zhi_list_update_slice)

}

func (this *TouTiaoProcesser) Finish() {
	base.Log.Info("zhihu表抓取解析完成 \r\n")

}

